package co.fitstart.mobile.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.course.ResultReport;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.user.bwh.BwhRecord;
import co.fitstart.mobile.service.ResultReportService;

/**
 * 成果报告
 * @author haiyan_xiao
 *
 */
@Service("resultReportService")
public class ResultReportServiceImpl implements ResultReportService {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.ResultReportService#listBwhRecord(co.fitstart.entity.course.round.CourseRound)
     */
    @SuppressWarnings("unchecked")
    public List<BwhRecord> listBwhRecord(CourseRound courseRound) {
        return genericDaoSupport.searchForList(" FROM BwhRecord WHERE disabled = false"
                + " AND courseRound = :courseRound AND week IS NOT NULL"
                + " ORDER BY createTime ASC", "courseRound", courseRound);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.ResultReportService#loadResultReport(co.fitstart.entity.course.round.CourseRound)
     */
    public ResultReport loadResultReport(CourseRound courseRound) {
        return genericDaoSupport.searchForObject("FROM ResultReport WHERE disabled = false"
                + " AND courseRound = :courseRound", "courseRound", courseRound, ResultReport.class);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.ResultReportService#editSummary(co.fitstart.entity.course.round.CourseRound, java.lang.String)
     */
    public void editSummary(CourseRound courseRound, String summary, String wordsToCoach) {
        ResultReport originalResultReport = loadResultReport(courseRound);
        
        if(originalResultReport == null) {
            originalResultReport = new ResultReport(courseRound);
            genericDaoSupport.save(originalResultReport.editStudentSummary(summary, wordsToCoach));
        } else {
            genericDaoSupport.update(originalResultReport.editStudentSummary(summary, wordsToCoach));
        }
    }
}
